package com.sky.controller.admin.api;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.sky.result.Result;
import com.sky.utils.MinioUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @program: sky-take-out
 * @description:
 * @author: 酷炫焦少
 * @create: 2024-11-16 16:11
 **/
@RestController
@RequestMapping("/admin/common")
@Api("文件相关接口")
public class FileController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            if (StrUtil.isBlank(originalFilename)) {
                return Result.error("文件名不能为空");
            }
            int index = originalFilename.lastIndexOf(".");
            String imageName = UUID.randomUUID().toString(true) + "." + originalFilename.substring(index + 1);
            String url = MinioUtil.uploadFile("img", imageName, file);
            return Result.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }
    }

}
